<?php
require('../includes/functions.inc.php');
require('../includes/LITTLE_ExcelReader.class.php');

ini_set("max_execution_time",0);
ini_set( "memory_limit" , "128M");

$smarty = SmartyManager::getSmarty();

/*************************************************************
 * 每个上传文件只要修改下面这些变量就基本可以了
 ************************************************************/
$formFilename = 'planFile';
//不可缺少的列
if($_POST['phase']==0){
	$obligatoCols = array(
		'序号','产品质量证明书号','发车证书号', '批次', '生产厂家', '船级', '材质', '厚', '宽', '长', 
		'数量', '单重', '重量', '订单号', '订单子项号', '受订单价', '物料号', '批号', '购单号', '发货时间', '合同月份',
		'车号', '销售产家', '备注'
	);

//电子表格和数据库表中的列的对应关系
$tableCols = array(
		'产品质量证明书号'	=> 'certificationNumber',
		'发车证书号'   => 'facheNumber',				
		'批次'              => 'sequenceNumber',
		'生产厂家'	        => 'manufactory',						
		'船级'	        => 'authenticationMark',					
		'材质'            =>  'steelNumber',						
		'厚'            =>  'size1',						
		'宽'			    =>  'size2',						
		'长'	            =>  'size3',						
		'数量'	            =>  'count',						
		'单重'	            =>  'unitWeight',						
		'订单号'	            =>  'orderNumber',						
		'订单子项号'	        =>  'orderSubitemNumber',						
		'受订单价'          =>	'unitPrice',						
		'物料号'            =>  'materialNumber',						
		'批号'	        =>  'sampleBatch',						
		'购单号'     	    =>  'purchaseNumber',						
		'发货时间'           =>  'shipmentDate',								
		'合同月份'			=>	'month',						
		'车号'	            =>  'vehicleNumber',						
		'销售产家'  	=>  'consignee',						
		'备注'	            =>  'remark'							
);

//对比时用来比较的列
$compareCols = array('facheNumber');

$tableName = 'little_main';

/************************************************************
 * 文件上传
 ***********************************************************/
try{
	//$file 在本程序中用作了全局变量，用来在需要filename的函数中使用
	$file = saveUploadedFile($formFilename);
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	exit();
}

try{
	beginTransaction();
	$littleReader = new LITTLE_ExcelReader();
	$littleReader->read($file->getNewPath());
	$littleReader->obligatoColumns = $obligatoCols;
	$littleReader->tableColumns = $tableCols;
	$littleReader->compareColumns = $compareCols;
	$littleReader->tableName = $tableName;

	//开始思考对于main上传，我们应该检查什么
	//1.一些必要的检查，查看第一行是否缺少某些必须的列
	//2.然后查看这个文件是否已经上传过`
	
	//1.
	$littleReader->checkFirstRow("main");
	//2.
	if($date = $littleReader->haveUploaded()){
		throw new UploadSameFileException($file->getFilename(), $date);
	}
	$littleReader->readIntoDB();
	LogInserter::logForInsert("导入总表文件：{$file->getFilename()}");
	
}catch(DBQueryException $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	rollback();
	exit();
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	rollback();
	exit();
}

}else if($_POST['phase']==2){
	$obligatoCols = array(
		'序号','材料代码', '批次', '生产厂家', '船级', '材质', '厚', '宽', '长', 
		'数量', '单重', '重量', '订单号', '订单子项号', '受订单价', '购单号','备注'
	);

//电子表格和数据库表中的列的对应关系
$tableCols = array(
		'材料代码'	=> 'materialCode',		
		'批次'              => 'sequenceNumber',
		'生产厂家'	        => 'manufactory',						
		'船级'	        => 'authenticationMark',					
		'材质'            =>  'steelNumber',						
		'厚'            =>  'size1',						
		'宽'			    =>  'size2',						
		'长'	            =>  'size3',						
		'数量'	            =>  'count',						
		'单重'	            =>  'unitWeight',						
		'订单号'	            =>  'orderNumber',						
		'订单子项号'	        =>  'orderSubitemNumber',						
		'受订单价'          =>	'unitPrice',			
		'购单号'     	    =>  'purchaseNumber',		
		'备注'	            =>  'remark'							
);

//对比时用来比较的列
$compareCols = array('materialCode');

$tableName = 'little_main3';

/************************************************************
 * 文件上传
 ***********************************************************/
try{
	//$file 在本程序中用作了全局变量，用来在需要filename的函数中使用
	$file = saveUploadedFile($formFilename);
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	exit();
}

try{
	beginTransaction();
	$littleReader = new LITTLE_ExcelReader();
	$littleReader->read($file->getNewPath());
	$littleReader->obligatoColumns = $obligatoCols;
	$littleReader->tableColumns = $tableCols;
	$littleReader->compareColumns = $compareCols;
	$littleReader->tableName = $tableName;

	//开始思考对于main上传，我们应该检查什么
	//1.一些必要的检查，查看第一行是否缺少某些必须的列
	//2.然后查看这个文件是否已经上传过`
	
	//1.
	$littleReader->checkFirstRow("main");
	
	//2.
	if($date = $littleReader->haveUploaded()){
		throw new UploadSameFileException($file->getFilename(), $date);
	}
	$littleReader->readIntoDB();
	LogInserter::logForInsert("导入总表3文件：{$file->getFilename()}");

}catch(DBQueryException $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	rollback();
	exit();
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	rollback();
	exit();
}

}
if(isset($warnMsg)){
	$smarty->assign('warnMsg', $warnMsg);
}
$smarty->assign('successMsg', $file->getFilename().'上传成功');
$smarty->display('success.html');
commit();
?>